package com.zyk.util;

import com.zyk.leetcode.ListNode;

/**
 * @author zhangsan
 * @date 2021/4/7 16:17
 */
public class ListUtil {

    public static ListNode generateList(int[] arr) {
        if (arr == null || arr.length == 0) {
            return null;
        }
        ListNode head = new ListNode(arr[0]);
        ListNode pre = head;
        ListNode cur = null;
        for (int i = 1; i < arr.length; i++) {
            cur = new ListNode(arr[i]);
            pre.next = cur;
            pre = pre.next;
        }
        return head;
    }

    public static ListNode generateList(int maxLen, boolean fixedSize) {
        maxLen = fixedSize ? maxLen : (int) (maxLen * Math.random() + 1);
        ListNode root = new ListNode((int) (Math.random() * 9 + 1));
        ListNode cur = root;
        int i = 1;
        while (i < maxLen) {
            cur.next = new ListNode((int) (Math.random() * 9 + 1));
            cur = cur.next;
            i++;
        }
        return root;
    }

    public static void print(ListNode head) {
        if(head == null) {
            return;
        }
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
}
